package com.wjw.redis.stream.controller;

import com.wjw.redis.stream.enums.RedisDelayQueueEnum;
import com.wjw.redis.stream.handler.RedissonDelayTaskHandler;
import com.wjw.redis.stream.model.MessageEvent;
import com.wjw.redis.stream.service.EventPublisher;
import com.wjw.redis.stream.service.TaskToDelayQueue;
import com.wjw.redis.stream.util.RedisDelayQueueUtil;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("order")
public class TestDelayQueueTask {

    @Autowired
    RedissonClient redissonClient;
    @Autowired
    TaskToDelayQueue addTaskToDelayQueue;
    @Autowired
    private RedisDelayQueueUtil redisDelayQueueUtil;

    @GetMapping("/queue/add")
    public void testRedissonDelayQueueOffer() {
        EventPublisher.publish(MessageEvent.builder().msg("测试消息队列").channel("test").build());

//        addTaskToDelayQueue.addTaskToDelayQueue("testRedissonDelayQueue");
        Map<String, String> map1 = new HashMap<>();
        map1.put("orderId", "100");
        map1.put("status", "Pay Timeout");
        map1.put("remark", "订单支付超时，自动取消订单");

        Map<String, String> map2 = new HashMap<>();
        map2.put("orderId", "200");
        map1.put("status", "Evaluation Timeout");
        map2.put("remark", "订单超时未评价，系统默认好评");

        // 添加订单支付超时，自动取消订单延迟队列。为了测试效果，延迟10秒钟
        redisDelayQueueUtil.addDelayQueue(map1, 10, TimeUnit.SECONDS, RedisDelayQueueEnum.ORDER_PAYMENT_TIMEOUT.getCode());

        // 订单超时未评价，系统默认好评。为了测试效果，延迟20秒钟
        redisDelayQueueUtil.addDelayQueue(map2, 20, TimeUnit.SECONDS, RedisDelayQueueEnum.ORDER_TIMEOUT_NOT_EVALUATED.getCode());
    }

//    @PostConstruct
//    void init() {
//        Thread threadOne = new Thread(new RedissonDelayTaskHandler(redissonClient));
//        threadOne.setName("redisson延迟任务线程");
//        threadOne.start();
//    }
}
